package com.example.administrator.blapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class listview extends AppCompatActivity {
    ListView mListView;
    private List<String> mStringList;
    private ArrayAdapter<String> mArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);

        mStringList = new ArrayList<String>();
        for(int i=0; i<10; i++)
        {
            mStringList.add(String.valueOf(i));
        }
        mListView = findViewById(R.id.lv);
        mListView.setFooterDividersEnabled(true);
        mListView.setHeaderDividersEnabled(false);

       // String[] county = getResources().getStringArray(R.array.citys);
        mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStringList);

        ImageView img = new ImageView(this);
        img.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        img.setImageResource(R.mipmap.bg);
        img.setScaleType(ImageView.ScaleType.FIT_XY);

        View footer = LayoutInflater.from(this).inflate(R.layout.lv_header, null);
        Button footer_button = footer.findViewById(R.id.bt_add);
        footer_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = mStringList.size();
                for(int i=index; i<index+10; i++)
                {
                    mStringList.add(String.valueOf(i));
                    mArrayAdapter.notifyDataSetChanged();
                }
            }
        });
        mListView.addFooterView(footer);

        mListView.addHeaderView(img);
        mListView.setAdapter(mArrayAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> var1, View var2, int var3, long var4) {
                Toast.makeText(listview.this, String.valueOf(var3), Toast.LENGTH_SHORT).show();
               // Toast.makeText(listview.this, "danji:" + var1.getItemAtPosition(var3).toString(), Toast.LENGTH_SHORT).show();
            }
        });

        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(listview.this, "long click:" + String.valueOf(i), Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }
}
